package com.ymatou.iapi.sellertrading.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellertrading.service.AcceptOrderCall;
import com.ymatou.iapi.sellertrading.service.CancelOrdersCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.YMTDateUtil;

public class Ts_CancelOrders {

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单无需补款");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new CancelOrdersCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrders_001() {
		Logger.start(true, "买手取消订单");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();
			//支付订单
			new OrderCallService().orderPayRequest(userid, payprice, tradingid);

			//把生成的订单号更新到脚本中
			List<Integer> list = new ArrayList<>();
			list.add(orderid);

			//买手取消订单
			Map<String, Object> updateMap = new HashMap<>();
			updateMap.put("OrderIds", new org.json.JSONArray(list));
			updateMap.put("Reason", "就是不喜欢");
			JSONObject resp = new CancelOrdersCall(sellerid, userid)
					.callAndGetReturnData(updateMap);
			//验证返回值
			Logger.verifyEquals("操作成功", resp.getString("Msg"), "操作成功");
			//验证sql
			DefaultWapper df = new DefaultWapper();
			Logger.verifyEquals(13,
					df.selectOrderByOderId(orderid).get(0)
							.get("iTradingStatus"), "订单表状态更新");
			//验证买手接单时间，误差不超过5秒
			AssertService.assertTime(
					YMTDateUtil.getDate(),
					YMTDateUtil.parseYMDHMSDate(df.selectOrderByOderId(orderid)
							.get(0).get("dCancelTime").toString()), 5);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrders_002() {
		Logger.start(false, "买手重复取消订单时失败");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();
			//支付订单
			new OrderCallService().orderPayRequest(userid, payprice, tradingid);

			//把生成的订单号更新到脚本中
			List<Integer> list = new ArrayList<>();
			list.add(orderid);

			//买手取消订单
			Map<String, Object> updateMap = new HashMap<>();
			updateMap.put("OrderIds", new org.json.JSONArray(list));
			updateMap.put("Reason", "就是不喜欢");
			JSONObject resp = new CancelOrdersCall(sellerid, userid)
					.callAndGetReturnData(updateMap);
			JSONObject resp1 = new CancelOrdersCall(sellerid, userid)
					.callAndGetReturnData(updateMap);
			//验证返回值
			Logger.verifyEquals(201, resp1.get("Code"), "接口返回201");
			Logger.verifyEquals(orderid + "此订单已取消~", resp1.get("Msg"), "失败原因");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrders_003() {
		Logger.start(false, "非订单所有者取消订单时失败");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();
			//支付订单
			new OrderCallService().orderPayRequest(userid, payprice, tradingid);

			//把生成的订单号更新到脚本中
			List<Integer> list = new ArrayList<>();
			list.add(orderid);

			//买手取消订单
			Map<String, Object> updateMap = new HashMap<>();
			updateMap.put("OrderIds", new org.json.JSONArray(list));
			updateMap.put("Reason", "就是不喜欢");
			int sellerid1 = sellerid + 1;
			JSONObject resp = new CancelOrdersCall(sellerid1, userid)
					.callAndGetReturnData(updateMap);
			//验证返回值
			Logger.verifyEquals(201, resp.get("Code"), "接口返回201");
			Logger.verifyEquals("用户" + sellerid1 + "不是订单" + orderid + "的所有者",
					resp.get("Msg"), "失败原因");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrders_004() {
		Logger.start(false, "发货的订单不能取消");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();
			//支付订单
			new OrderCallService().orderPayRequest(userid, payprice, tradingid);
			//买手接单
			Map<String, Object> ac = new HashMap<String, Object>();
			ac.put("OrderId", orderid);
			JSONObject response = new AcceptOrderCall(sellerid, userid)
					.callAndGetReturnData(ac);
			//订单发货
			new OrderCallService().dispatched(orderid, sellerid);
			//把生成的订单号更新到脚本中
			List<Integer> list = new ArrayList<>();
			list.add(orderid);

			//买手取消订单
			Map<String, Object> updateMap = new HashMap<>();
			updateMap.put("OrderIds", new org.json.JSONArray(list));
			updateMap.put("Reason", "就是不喜欢");
			JSONObject resp = new CancelOrdersCall(sellerid, userid)
					.callAndGetReturnData(updateMap);
			//验证返回值
			Logger.verifyEquals(201, resp.get("Code"), "接口返回201");
			Logger.verifyEquals("卖家取消订单" + orderid + "状态不匹配", resp.get("Msg"),
					"错误原因");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrders_005() {
		Logger.start(false, "已经完成的订单不能取消");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();
			//支付订单
			new OrderCallService().orderPayRequest(userid, payprice, tradingid);
			//买手接单
			Map<String, Object> ac = new HashMap<String, Object>();
			ac.put("OrderId", orderid);
			JSONObject response = new AcceptOrderCall(sellerid, userid)
					.callAndGetReturnData(ac);
			//订单发货
			new OrderCallService().dispatched(orderid, sellerid);
			//确认收货
			new OrderCallService().confirmReceive(userid, orderid);
			//把生成的订单号更新到脚本中
			List<Integer> list = new ArrayList<>();
			list.add(orderid);

			//买手取消订单
			Map<String, Object> updateMap = new HashMap<>();
			updateMap.put("OrderIds", new org.json.JSONArray(list));
			updateMap.put("Reason", "就是不喜欢");
			JSONObject resp = new CancelOrdersCall(sellerid, userid)
					.callAndGetReturnData(updateMap);
			//验证返回值
			Logger.verifyEquals(201, resp.get("Code"), "接口返回201");
			Logger.verifyEquals("卖家取消订单" + orderid + "状态不匹配", resp.get("Msg"),
					"错误原因");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrders_006() {
		Logger.start(false, "退货退款的订单不能取消");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();
			//支付订单
			new OrderCallService().orderPayRequest(userid, payprice, tradingid);
			//买手接单
			Map<String, Object> ac = new HashMap<String, Object>();
			ac.put("OrderId", orderid);
			JSONObject response = new AcceptOrderCall(sellerid, userid)
					.callAndGetReturnData(ac);
			//退货退款
			new OrderCallService().applySalesRefundRequestInJava(orderid);
			//把生成的订单号更新到脚本中
			List<Integer> list = new ArrayList<>();
			list.add(orderid);

			//买手取消订单
			Map<String, Object> updateMap = new HashMap<>();
			updateMap.put("OrderIds", new org.json.JSONArray(list));
			updateMap.put("Reason", "就是不喜欢");
			JSONObject resp = new CancelOrdersCall(sellerid, userid)
					.callAndGetReturnData(updateMap);
			//验证返回值
			Logger.verifyEquals(201, resp.get("Code"), "接口返回201");
			Logger.verifyEquals("哈尼，" + orderid + "此单有退货/退款，请先去处理~",
					resp.get("Msg"), "错误原因");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrders_007() {
		Logger.start(true, "没有支付的订单取消订单");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerid = placeOrderResp.getOrderList().get(0).getSellerId();
			int userid = placeOrderResp.getUserId();
			int tradingid = placeOrderResp.getTradingId();
			BigDecimal payprice = placeOrderResp.getPayAmount();
			//把生成的订单号更新到脚本中
			List<Integer> list = new ArrayList<>();
			list.add(orderid);

			//买手取消订单
			Map<String, Object> updateMap = new HashMap<>();
			updateMap.put("OrderIds", new org.json.JSONArray(list));
			updateMap.put("Reason", "就是不喜欢");
			JSONObject resp = new CancelOrdersCall(sellerid, userid)
					.callAndGetReturnData(updateMap);
			//验证返回值
			Logger.verifyEquals(200, resp.get("Code"), "接口返回正常");
			//验证sql
			DefaultWapper df = new DefaultWapper();
			Logger.verifyEquals(13,
					df.selectOrderByOderId(orderid).get(0)
							.get("iTradingStatus"), "订单表状态更新");
			//验证买手接单时间，误差不超过5秒
			AssertService.assertTime(
					YMTDateUtil.getDate(),
					YMTDateUtil.parseYMDHMSDate(df.selectOrderByOderId(orderid)
							.get(0).get("dCancelTime").toString()), 5);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
